package coreservlets.ch7;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import coreservlets.ServletUtilities;

public class PrimeNumberServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ArrayList<PrimeList> primeListCollection = new ArrayList<PrimeList>();
	private int maxPrimeLists = 30;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int numPrimes = ServletUtilities.getIntParameter(request, "numPrimes", 50);
		int numDigits = ServletUtilities.getIntParameter(request, "numDigits", 120);
		PrimeList primeList = findPrimeList(primeListCollection, numPrimes, numDigits);
		if (primeList == null) {
			primeList = new PrimeList(numPrimes, numDigits, true);
			// Multiple servlet request threads share the instance
			// variables (fields) of PrimeNumbers. So
			// synchronize all access to servlet fields.
			synchronized (primeListCollection) {
				if (primeListCollection.size() >= maxPrimeLists)
					primeListCollection.remove(0);
				primeListCollection.add(primeList);
			}
		}
		ArrayList<BigInteger> currentPrimes = primeList.getPrimes();
		int numCurrentPrimes = currentPrimes.size();
		int numPrimesRemaining = (numPrimes - numCurrentPrimes);
		boolean isLastResult = (numPrimesRemaining == 0);
		if (!isLastResult) {
			response.setIntHeader("Refresh", 5);
		}
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String title = "Some " + numDigits + "-Digit Prime Numbers";
		out.println(ServletUtilities.headWithTitle(title)
				+ "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H2 ALIGN=CENTER>" + title
				+ "</H2>\n" + "<H3>Primes found with " + numDigits
				+ " or more digits: " + numCurrentPrimes + ".</H3>");
		if (isLastResult)
			out.println("<B>Done searching.</B>");
		else
			out.println("<B>Still looking for " + numPrimesRemaining
					+ " more<BLINK>...</BLINK></B>");
		out.println("<OL>");
		for (int i = 0; i < numCurrentPrimes; i++) {
			out.println(" <LI>" + currentPrimes.get(i));
		}
		out.println("</OL>");
		out.println("</BODY></HTML>");
	}

	private PrimeList findPrimeList(ArrayList<PrimeList> primeListCollection, int numPrimes, int numDigits) {
		for (int i = 0; i < primeListCollection.size(); i++) {
			PrimeList primeList = (PrimeList) primeListCollection.get(i);
			synchronized (primeListCollection) {
				if (numDigits == primeList.numDigits() && numPrimes == primeList.numPrimes()) {
					return primeList;
				}
			}
		}
		return null;
	}

}
